const arrayProto = Array.prototype
import { def } from './observer.js'
export const arrayMethods = Object.create(arrayProto);
['push', 'shift', 'unshift', 'pop', 'splice', 'sort', 'reverse'].forEach(function (method) {
  //保存数组的原始方法
  const original = arrayProto[method]
  //通知依赖
  def(arrayMethods, method, function mutator (...args) {
    const result = original.apply(this, args)
    const ob = this.__ob__
    let inseted
    switch (method) {
      case 'push':
      case 'unshift':
        inseted = args
        break
      case 'splice':
        inseted = args.slice(2)
        break
    }
    if (inseted) {
      ob.observeArray(inseted)
    }
    ob.dep.notify()
    return result
  })
  console.log(original);
  Object.defineProperty(arrayMethods, method, {
    value: function mutator (...args) {
      const result = original.apply(this, args)
      const ob = this.__ob__
      console.log(ob);
      //向依赖发送消息
      ob.dep.notify()
      // return original.apply(this, args)
      return result
    },
    enumerable: false,
    writable: true,
    configurable: true
  })
})